﻿// Author:				    Joe Audette
// Created:			        2010-05-22
// Last Modified:		    2010-05-23
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections;
using mojoPortal.Business;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using Resources;
using ud_Article.Business;

namespace ud_Article.UI
{
    public partial class ViewListByCategory : mojoBasePage
    {
        protected int pageId = -1;
        protected int moduleId = -1;
        private int categoryId = -1;
        private bool userCanEdit;
        private int pageNumber = 1;
        private ArticleConfiguration config = new ArticleConfiguration();

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            Load += Page_Load;
            base.OnInit(e);
        }


        #endregion

        private void LoadPanels()
        {
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting, config.ShowBottomPanelSetting, config.ShowNumberModuleSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!UserCanViewPage(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            LoadSettings();
            PopulateControls();

        }

        private void PopulateControls()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, ArticleResources.PostList);
            postList.ModuleId = moduleId;
            postList.PageId = pageId;
            postList.IsEditable = userCanEdit;
            postList.SiteRoot = SiteRoot;
            postList.ImageSiteRoot = ImageSiteRoot;
            postList.Config = config;
            postList.CategoryId = categoryId;
            //make this page look as close as possible to the way a cms page with the blog module on it looks

        }


        private void LoadSettings()
        {
            userCanEdit = UserCanEditModule(moduleId);
            if (userCanEdit) { Article.CountOfDrafts(moduleId); }
            pnlWrapper.CssClass += config.ViewListCssClass != string.Empty
                                       ? " " + config.ViewListCssClass
                                       : " " + config.InstanceCssClass;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            pageNumber = WebUtils.ParseInt32FromQueryString("pagenumber", pageNumber);
            categoryId = WebUtils.ParseInt32FromQueryString("cat", categoryId);
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new ArticleConfiguration(settings);
            AnalyticsSection = ConfigHelper.GetStringProperty("AnalyticsBlogSection", "blog");
        }
    }
}